<?php
namespace Admin\Controller;
use Think\Controller;
class CategoryController extends Controller {
	
	
	//分类列表
	public function listAction(){
		$Model=D("Category"); // 实例化admin对象
		
		$list=$this->getcateg(0);

	
		$this->assign('list',$list);// 赋值数据集

		$this->display();
		}
	//分类增加
	public function saveAction(){
		$Model=D("Category"); // 实例化admin对象
		
		//编辑
		$id=I('get.id'); // 取ID
		$one=$Model->where(" catid=$id ")->find();
		$this->assign('one', $one);
		
		$lang=empty($id)?'增加':'编辑';
		$this->assign('lang', $lang);
		
		$list=$this->getcateglist(0);
		$this->assign('list', $list);
		
		//编辑
		if(!empty($id)){
			$catid=$this->getchinid($id);
			$temp=$catid;
			do{
				$pid=$temp['parentid'];	
				$retult[$temp['catid']]=$this->getcateglist($pid);	
				$temp=$this->getchinid($pid);	
				}while($pid);
				$retult=array_reverse($retult, true);
		}
		$this->assign('retult', $retult);

		$data=I('post.'); 
		
		if(!empty($data)){
			
			
			if(empty($id)){
				//增加分类
				$insert=$Model->data($data)->add();
				if($insert){
						$this->success('增加成功', U('category/list'));
					}else{
					 $this->error('增加失败');
					}
				}else{
					//编辑分类
				 	$edit=$Model->where(" catid=$id ")->data($data)->save();
					if($edit){
							$this->success('编辑成功', U('category/list'));
							die();
						}else{
					 		$this->error('编辑失败');
							die();
						}
					}
			}
			$this->display();
		}
		
	public function ajaxAction(){
		$pid=I('post.parentid');
		$retul=$this->getcateglist($pid);
		
		if(IS_AJAX){
			$this->ajaxReturn($retul);
			}else{
				echo array();
				}
		
		
		}	

		
	//分类删除
	public function delAction(){
		$Model=D("Category"); // 实例化对象
		$id=I('get.id'); // 取ID
		$one=$Model->where(' catid='.$id)->find();
		if(!empty($one)){
			$list=$this->getchinkid($id);
			$map['catid']  = array('in',$list);
			$del=$Model->where($map)->delete();
			if($del){
				$this->success('删除成功', U('category/list'));
				die();
				}else{
				$this->error('删除失败');
				die();
				}
			
		}	
	}
	
	
	//读取分类树-------------------------------
	public function getcateg($pid,$retul=array(),$k=0){
		$Model=D("Category"); // 实例化对象
		
		
		$list=$Model->where(' parentid='.$pid)->select();
		
		
		foreach($list as $key=>$val){
			
			$chin=$this->getchin($val['catid']);
			if(empty($chin)){
					$chh=0;
				}else{
					$chh=1;
				}
			$val['chh']=$chh;
			
			$str='';
			for($i=0;$i<$k;$i++){
				$str.='|-';
				}
			$val['name']=$str.$val['name'];
					
			$retul[]=$val;	
			$retul=$this->getcateg($val['catid'],$retul,$k+1);
			
		}
		 return $retul;
	}
	
	
	public function getchin($pid){
		$Model=D("Category"); // 实例化对象		
		return $row=$Model->where(' parentid='.$pid)->find();
	}
	
	
	public function getcateglist($pid){
		$Model=D("Category"); // 实例化对象		
		$retul=$Model->where(' parentid='.$pid)->select();
		 return $retul;
	}
	
	
	public function getchinid($catid){
		$Model=D("Category"); // 实例化对象	
		$retul=$Model->where(' catid='.$catid)->find();	
		 return $retul;
	}
	

		/*
	查找分类下的所有子分类
	*/
	public function getchinkid($id,$array=array()){
		$Model=D("Category"); // 实例化对象			
		$array[]=$id;
		$Tlist=$Model->where(' parentid='.$id)->select();
		foreach($Tlist as $key=>$val){
			$array=$this->getchinkid($val['catid'],$array);
			}
			return $array;
	}
	
	
	//-------------------------

	
	//批量操作
	public function batchdelAction(){
		$Model=D("Category"); // 实例化admin对象
		/*批量删除*/
		$batch=I('post.del');
		if(!empty($batch)){
			$id=I('post.id');
			foreach($id as $key=>$val){	
				$Model->where(" catid=$val ")->delete(); // 删除id为什么的用户数据		
				}
		}
		
		/*批量审核*/
		$batch=I('post.status1');
		if(!empty($batch)){	
			$id=I('post.id');
			foreach($id as $key=>$val){		
				$Model->where(" catid=$val ")->save($data=array('status'=>1)); // 根据条件更新记录
				}
		}
		
		/*批量未审核*/
		$batch=I('post.status0');
		if(!empty($batch)){	
			$id=I('post.id');
			foreach($id as $key=>$val){
				$Model->where(" catid=$val ")->save($data=array('status'=>0)); // 根据条件更新记录
				}
		}
		
		$this->success('操作成功', U('category/list'));
		
	}		
	
}